package com.huazi.comservice.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.huazi.commonutils.CommentResult;
import com.huazi.comservice.entity.EduComment;
import com.huazi.comservice.service.EduCommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 评论 前端控制器
 * </p>
 *
 * @author huazi
 * @since 2021-03-30
 */
@RestController
@CrossOrigin
@RequestMapping("/comservice/edu-comment")
public class EduCommentController {
    @Autowired
    EduCommentService commentService;
    //根据课程id获取所有评论
    @GetMapping("/getCourseComment/{id}/{current}")
    public CommentResult getCourseComment(@PathVariable String id,@PathVariable Integer current){
        Page<EduComment> page=new Page<>(current,3);
        commentService.page(page,new QueryWrapper<EduComment>().eq("course_id", id).orderByDesc("gmt_create"));
        return CommentResult.getOK().data("comments",page);
    }
    //添加一条评论
    @PostMapping("/addCourseComment")
    public CommentResult addCourseComment(@RequestBody EduComment comment){
        commentService.save(comment);
        return CommentResult.getOK();
    }
    //根据课程id获取评论数
    @GetMapping("/getCommentCount/{id}")
    public Integer getCommentCount(@PathVariable String id){
        int count = commentService.count(new QueryWrapper<EduComment>().eq("course_id", id));
        return count;
    }
}

